var apiUrl=contextPath+"/customer/distribution/list";
var apiUrl_dfp=contextPath+"/call/callCustomer/getWeifenpeiPageModel";
Vue.http.options.emulateJSON = true;
var listVue=new Vue({
  el: '#fpglProjectIndex',
  data: {
	  //已分配查询参数
	  params:{
		  pageSize:10,
		  pageNum:1,
		  projectId:'',
		  assignPerson:'',
		  startTimeStart:'',
		  startTimeEnd:'',
		  customerStatusArray:[],
		  customerOriginArray:[],
		  intentionLevelArray:[],
		  customerStatus:'',
		  customerOrigin:'',
		  intentionLevel:'',
		  createUserId:'',
		  assignPerson:''
	  },
	  //待分配查询参数
	  params1:{
		  pageSize:10,
		  pageNum:1,
		  wfpProjectId:''
	  },
	  //分配表单
	  distribution:{
		  ids:'',
		  customerId:'',
		  saleGroupId:''
	  },
	  permission:{
		  query:false,
		  query1:false,
		  distribution:false,
		  batchDistribution:false,
		  callCustomerShow:true
	  },
  	  ppList:[],
  	  dfpList:[],
  	  projectList:[],
  	  assignPersonList:[],
  	  createUserIdList:[],
  	  intentionLevelList:[],
  	  customerStatusList:[],
  	  customerOriginList:[],
  	  saleGroupList:[],
  	  agentList:[]
  },
  mounted:function(){
	  //分配管理,待分配是否显示
	  this.initToDist();
	  //初始化js
	  this.jsInit();
	  //初始化项目
	  this.initProject();
	  //加载枚举
	  this.initPageEnum(1);//客户等级
	  this.initPageEnum(2);//客户状态
	  this.initButtons();
	  
  },
  watch:{
	  'params.projectId':function(curVal,oldVal){
		 this.initAllUser();
		 this.initCustomerOrigin();
	  },
	  'distribution.saleGroupId':function(value){
		  this.initAgent();
	  }
  },
  methods: {
	  jsInit:function(){
		  $(".form_datetime").datetimepicker({
	          format: "yyyy-mm-dd",//日yyyy-mm-dd hh:ii
	          autoclose: true,
	          todayBtn: true,
	          minView:2,//显示到天设置2、显示到时设置1、显示到分设置0
	          startView:2,//首先显示的视图
	          pickerPosition: "bottom-left",
			  language: 'zh'
	      });
		  
		  
	        $(".nav-list-cell li").click(function(){
	            $(this).addClass("active").siblings().removeClass("active");
	            var num = $(this).index();
	            showInfoDtails(num);
	        })
	        function showInfoDtails(index){
	            $(".info-details-list .details-list-li").eq(index).addClass("active").siblings().removeClass("active");
	        }
	        $(".across-show").hover(function(){
	            $(this).tooltip('show')
	        })
	        
	        
	  },
	  getSplitArray:function(array){
		    var status="";
	    	for(var i = 0;i<array.length;i++){
	    		 var rowValue=array[i];
	    		 if(i>0){
	    			 status=status+",";
	    		 }
	    		 status=status+rowValue;
	    	}
	    	return status;
	  },
	  queryList:function(){
		  this.params.pageNum=1;
		  this.getbaseList();
	  },
	  //已分配列表
	  getbaseList:function() {
	    	var _self = this;
	    	if(_self.params.projectId==""){
	    		layer.tips('项目必选', '#projectId', {
	    			  tips: [2, '#FF5722'],
	    			  time: 2000
	    		});
	    		return;
	    	}
	    	var loadIndex = layer.load(0, {shade: false});
	    	var queryParams=_self.params;
	    	var customerStatus=_self.getSplitArray(_self.params.customerStatusArray);
	    	var customerOrigin=_self.getSplitArray(_self.params.customerOriginArray);
	    	var intentionLevel=_self.getSplitArray(_self.params.intentionLevelArray);
	    	var startTimeStart=$("#startTimeStart").val();
	    	var startTimeEnd=$("#startTimeEnd").val();
	    	queryParams.customerStatus=customerStatus;
	    	queryParams.customerOrigin=customerOrigin;
	    	queryParams.intentionLevel=intentionLevel;
	    	queryParams.startTimeStart=startTimeStart;
	    	queryParams.startTimeEnd=startTimeEnd;
	     	$.ajax({
	    		type:'post',
	            url: apiUrl,
	    	    data:queryParams,
	    	    dataType:'json',
	    	    success: function(res){
	    	    	layer.close(loadIndex);
	    	    	if(res.res=="1"){
	    	    		var rows=res.obj.rows;
	        	    	_self.ppList=rows;
	        	    	//初始化分页
	                	var pages=res.obj.totalPages;
	    	        	var current=res.obj.pageNumber;
	            	    laypage({
	            		  cont: $('#pagination'), //容器。值支持id名、原生dom对象，jquery对象,
	            		  pages: pages, //总页数
	            		  curr:current,
	            		  skip: true, //是否开启跳页
	            		  skin: 'molv',
	            		  groups: 3, //连续显示分页数
	            		  jump: function(obj,first){
	            			  if(!first){
	            				  _self.params.pageNum=obj.curr;
	            				  _self.getbaseList();
	            			  }
	            		  }
	            		});
	            	    
	    	    	}else{
	    	    		layer.close(loadIndex);
	    	    		layer.alert(res.resMsg);
	    	    	}
	    	    	
	    	    },
	    	    error:function(res){
	    	    	
	    	    }
	    	});   
	     },
	     queryDfpList:function(){
	    	 this.params1.pageNum=1;
	    	 this.getDfpList();
	     },
	     //待分配列表
	     getDfpList:function() {
	    	var _self = this;
	    	if(_self.params1.wfpProjectId==""){
	    		layer.tips('项目必选', '#wfpProjectId', {
	    			  tips: [2, '#FF5722'],
	    			  time: 2000
	    		});
	    		return;
	    	}
	    	var loadIndex = layer.load(0, {shade: false});
	     	$.ajax({
	    		type:'post',
	            url: apiUrl_dfp,
	    	    data:_self.params1,
	    	    dataType:'json',
	    	    success: function(res){
	    	    	layer.close(loadIndex);
	    	    	if(res.res=="1"){
	    	    		var rows=res.obj.rows;
	        	    	_self.dfpList=rows;
	        	    	for(var i=0;i<_self.dfpList.length;i++){
	        	    		var source = _self.dfpList[i].source;
	        	    		_self.dfpList[i].source = "call客";
	        	    		if(source){
	        	    			if("2" == source){
	        	    				_self.dfpList[i].source = "渠道转客";
	        	    			}
	        	    		}
	        	    	}
	        	    	//初始化分页
	                	var pages=res.obj.totalPages;
	    	        	var current=res.obj.pageNumber;
	            	    laypage({
	            		  cont: $('#pagination_dfp'), //容器。值支持id名、原生dom对象，jquery对象,
	            		  pages: pages, //总页数
	            		  curr:current,
	            		  skip: true, //是否开启跳页
	            		  skin: 'molv',
	            		  groups: 3, //连续显示分页数
	            		  jump: function(obj,first){
	            			  if(!first){
	            				  _self.params1.pageNum=obj.curr;
	            				  _self.getDfpList();
	            			  }
	            		  }
	            		});
	            	    
	    	    	}else{
	    	    		layer.close(loadIndex);
	    	    		layer.alert(res.resMsg);
	    	    	}
	    	    	
	    	    },
	    	    error:function(res){
	    	    	
	    	    }
	    	});   
	     },
	     //初始化项目
	     initProject:function(){
			var _self = this;
			var url=contextPath+"/estate/project/findProjectList?resourcesCode=menu_0003_0001";
			$.ajax({
				type: 'post',
				url: url,
				data: {
					resourcesCode:'menu_0003_0001',
					resourcesName:'resName_pro_list',
					needAuth:'T'
				},
				dataType:'json',
				success: function(res){
					var list=res.root;
					_self.projectList=list;
				}
			});
	     },
	   //初始化用户
	    initAllUser:function(){
    	    var _self = this;
    	    var projectId =  _self.params.projectId;
    	    if(projectId==null||projectId==""){
    	    	return;
    	    }
    	    //var url=contextPath+"/customer/customer/returnGroupUserList";
    	    var url=contextPath+"/customer/customer/userByProjectIdForGroup";
    		$.ajax({
    			type: 'post',
    	        url: url,
    		    data: {
    		    	resourcesCode:'menu_0006_0001',
    		    	projectId:projectId
    		    },
    		    dataType:'json',
    		    success: function(res){
    		    	var list=res;
    		    	//console.log(list);
    		    	_self.assignPersonList=list;
    		    	_self.createUserIdList=list;
    		    }
    		});
	     },
	     //初始化字典
	  	initPageEnum:function(value){
			var _self = this;
			var url = contextPath+"/router/customer/getEnumJson";
			var enumParams = {
					code:value
			};
			_self.$http.post(url,enumParams).then((response)=>{
				switch(value){
				case 1:
					_self.intentionLevelList=response.body.obj;
				  break;
				case 2:
					_self.customerStatusList=response.body.obj;
				  break;
				case 4:
					_self.customerOriginList=response.body.obj;
				  break;
				}
				
			});
	   },
      initCustomerOrigin:function(value){
          var _self = this;
          var projectId = _self.params.projectId;
          var url = contextPath+"/system/baseCodeValue/getCodeNameByType";
          var enumParams = {
			  delFlag: "0",
			  projectId: projectId,
              typeValue: 'customerNature'
          };
          _self.$http.post(url,enumParams).then((response)=>{
              _self.customerOriginList=response.body;
          });
      },
	   initSaleGroup:function(){
		   var _self=this;
		   var projectId=_self.params1.wfpProjectId;
			$.ajax({
				type: 'post',
		        url: contextPath+'/customer/customer/returnGroupList',
			    data: {
			    	"resourcesCode":"menu_0006_0001",
			    	"projectId":projectId
			    },
			    dataType:'json',
			    success: function(res){
			    	_self.saleGroupList=res;
			    }
			});
	   },
	   initAgent:function(){
		    var _self=this;
			var salesGroupId = _self.distribution.saleGroupId;
			$.ajax({
				type: 'post',
		        url: contextPath+'/customer/customer/returnGroupUserList',
			    data: {
			    	"resourcesCode":"menu_0006_0001",
			    	"salesGroupId":salesGroupId
			    },
			    dataType:'json',
			    success: function(res){
			    	_self.agentList=res;
			    }
			});
	   },
	   showDistribution:function(id){
		   if(this.params1.wfpProjectId==""){
	    		layer.tips('项目必选', '#wfpProjectId', {
	    			  tips: [2, '#FF5722'],
	    			  time: 2000
	    		});
	    		return;
	    	}
		   //清除原来数据
		   this.distribution.customerId='';
		   this.distribution.saleGroupId='';
		   this.agentList=[];
		   this.saleGroupList=[];
		   this.initSaleGroup();
		   this.distribution.ids=id;
		   $('#myModalTwo').modal('show');
	   },
	   saveDistribution:function(){
		    var _self=this;
			var ids = _self.distribution.ids;
			var customerId = _self.distribution.customerId;
			var projectId=_self.params1.wfpProjectId;
			$.ajax({
				type: 'post',
		        url: contextPath+'/customer/distribution/distributionConsultant',
			    data: {
			    	ids:ids,
			    	projectId:projectId,
			    	customerId:customerId
			    },
			    dataType:'json',
			    success: function(res){
			    	if(res.res== "1"){
			    		$('#myModalTwo').modal('hide');
			    		layer.alert("分配成功");
			    		_self.getDfpList(); //刷新列表
			    	}else if(res.res== "100"){
			    		layer.alert("权限不足");
			    	}else{
			    		layer.alert("分配失败");
			    	}
			    }
			});
	   },
	   showBatchDistribution:function(){
		   if(this.params1.wfpProjectId==""){
	    		layer.tips('项目必选', '#wfpProjectId', {
	    			  tips: [2, '#FF5722'],
	    			  time: 2000
	    		});
	    		return;
	    	}
		   var ids="";
		   $('input[name="checkbox"]:checked').each(function(){ 
			   if(ids!=""){
				   ids=ids+",";
			   }
			   ids=ids+$(this).val();
		   });
		   if(ids==""){
			  layer.msg("请勾选待分配记录"); 
			  return; 
		   }
		   this.distribution.customerId='';
		   this.distribution.saleGroupId='';
		   this.agentList=[];
		   this.saleGroupList=[];
		   this.initSaleGroup();
		   this.distribution.ids=ids;
		   $('#myModalTwo').modal('show');
	   },
	   allCheck:function(){
		   var check=$("#allOrNoCheck").prop("checked");
		   if(check==false){
			   $("input[name = checkbox]:checkbox").prop("checked", false);
		   }else{
			   $("input[name = checkbox]:checkbox").prop("checked", true);
		   } 
	   },
	   initButtons:function(){
		   var _self=this;
		   var parentCode="menu_0006_0004";
		   $.ajax({
				type: 'post',
		        url: contextPath+'/common/permission/getButtons',
			    data: {
			    	parentCode:parentCode
			    },
			    dataType:'json',
			    success: function(res){
			    	if(res.res== "1"){
			    		var buttons=res.obj;
			    		var query="customer:distribution:list";
			    		var query1="customer:distribution:list1";
			     		var distribution="customer:distribution:distribution";
			     		var batchDistribution="customer:distribution:batchDistribution";
			     		for(ob in buttons){
			     			var code=buttons[ob];
			     			if(code==query){
			     				_self.permission.query=true;
			     			}else if(code==query1){
			     				_self.permission.query1=true;
			     			}else if(code==distribution){
			     				_self.permission.distribution=true;
			     			}else if(code==batchDistribution){
			     				_self.permission.batchDistribution=true;
			     			}
			     		}
			    	}
			    }
			});
	   },
	   initToDist:function(){
		   var _self=this;
		   $.ajax({
				type: 'post',
		        url: contextPath+'/common/permission/getToDist',
			    data: {
			    },
			    dataType:'json',
			    success: function(res){
			    	console.log(res);
			    	if(res.res== "1"){
			    		var obj = res.obj;
			    		if(obj == true){
			    			_self.permission.callCustomerShow = obj;
			    		}
			    	}
			    }
			});
	   },
	   clearForm: function(){
		   this.params.projectId = "";
		   this.params.assignPerson = "";
		   this.params.createUserId = "";
		   $("#startTimeStart").val("");
		   $("#startTimeEnd").val("");
		   $("input[name='customerStatus']").each(function(){
			   $(this).prop("checked", false);
		   })
		   $("input[name='intentionLevel']").each(function(){
			   $(this).prop("checked", false);
		   })
		   $("input[name='customerOrigin']").each(function(){
			   $(this).prop("checked", false);
		   })
	   }
  }
})